#ifndef USER_H
#define USER_H

#include <QObject>
#include <QTcpSocket>
#include <QTime>
#include <QTimer>

#include "audiosink.h"
#include "audioformat.h"

class User : public QObject
{
    Q_OBJECT
public:
    struct userConfig {
        int maxBufferUnderflows;
        bool kickAfk;
        int kickAfkTime;
        bool kickUnderFlow;
        int maxBufferSize;
        int bufferSize;
        AudioFormat format;
        QString alias;
        int deviceId;
        bool banned;
        bool sockConfig;
        bool muted;
        struct TcpRelayConfig {
            bool useRelay;
            QString host;
            int port;
        };
        TcpRelayConfig tcpRelay;
        QTime connectTime;
        AudioSink::Mode currentMode;
    };
    explicit User(QTcpSocket *socket, userConfig config,QTextStream* out = 0, QObject *parent = 0);
    ~User();
    void kill(const QString reason);
    void setTextOutput(QTextStream *out);
    QHostAddress getHost();
    QString getAlias();
    void sendServerInfo();
    userConfig getConfig();
    quint64 getReadedData();
    quint64 getSpeedAverage();

private:
    QTextStream *out;
    QTcpSocket* sock;
    AudioSink* output;
    quint64 bytesCount;
    quint64 lastBytesCount;
    quint64 checkCount;
    int bufferUnderflows;
    QTime connectTime;
    QTimer *checker;
    void say(const QString message);
    userConfig config;
    QByteArray buffer;
    QString timeStamp;
    QString alias;
    bool readUserConfig(QByteArray *data);
    void reconfigureSink();
    int bitrate;

signals:
    void disconnected(User*);

public slots:
    void sockClose();
    void sockRead();
    void sockError();
    void sockCheck();
};

#endif // USER_H
